package com.tabview.difficultpoint;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xudow.app.Config;
import com.xudow.app.R;

import java.io.File;
import java.util.List;

/**
 * 作者：zhouXJ on 2016/3/30 11:34
 */
public class CourseDifficultPointHolder {
    String title;
    private Context context;
    private RelativeLayout course_difficult_point_item_rl;
    private TextView course_difficult_point_item_name;
    private LayoutInflater inflater;
    private LinearLayout course_difficult_point_item_content_ll;
    private ImageView open_file_list_icon_img;
    private boolean isOpen = false;
    private LinearLayout course_difficult_point_item_flg_ll;


    private List<CourseDifficultPointSubContent> list;
    private String suffixName;
    private String fileUrl;
    private ListView listView;
    private String savePath = Config.SDCARD_ROOT_DIR + "/update/";
    private String filePath;


    public CourseDifficultPointHolder(View view, Context context, ListView listView,String t) {
        this.context = context;
        this.listView = listView;
        this.title=t;
        course_difficult_point_item_rl = (RelativeLayout) view.findViewById(R.id.course_difficult_point_item_rl);
        course_difficult_point_item_name = (TextView) view.findViewById(R.id.course_difficult_point_item_name);
        course_difficult_point_item_content_ll = (LinearLayout) view.findViewById(R.id.course_difficult_point_item_content_ll);
        open_file_list_icon_img = (ImageView) view.findViewById(R.id.open_file_list_icon_img);
        course_difficult_point_item_flg_ll = (LinearLayout) view.findViewById(R.id.course_difficult_point_item_flg_ll);
        inflater = LayoutInflater.from(context);
    }

    public void bind(final CourseDifficultPointContent point, final int position) {
        final String times="第" + point.getTimes() + "次课";
        list = point.getAttachmentVOs();
        final String agencyId = point.getAgencyid();
        course_difficult_point_item_content_ll.removeAllViews();
        if (list != null && list.size() > 0) {
            course_difficult_point_item_flg_ll.setVisibility(View.VISIBLE);
            for (int i = 0; i < list.size(); i++) {
                View contentView = inflater.inflate(R.layout.course_difficult_point_item_content_layout, null);
                TextView course_difficult_point_item_content_name = (TextView) contentView.findViewById(R.id.course_difficult_point_item_content_name);
                FrameLayout difficult_point_item_content_fl = (FrameLayout) contentView.findViewById(R.id.difficult_point_item_content_fl);
                LinearLayout difficult_point_item_content_ll = (LinearLayout) contentView.findViewById(R.id.difficult_point_item_content_ll);
                ImageView course_difficult_point_item_content_img = (ImageView) contentView.findViewById(R.id.course_difficult_point_item_content_img);
                TextView difficult_point_item_content_open = (TextView) contentView.findViewById(R.id.difficult_point_item_content_open);
                String name = list.get(i).getName();
                String url = list.get(i).getUrl();

                String fileName =title+"_"+times+"_"+name;
                filePath = savePath + fileName; // 文件路径

                fileUrl = Config.BASE_URL + "/app/study/downloadAttachment?agencyId="
                        + agencyId + "&&url=" + url + "&name=" + name;
                difficult_point_item_content_ll.setTag(list.get(i));
                if (!TextUtils.isEmpty(name)) {
                    if (name.contains(".")) {
                        String n[] = name.split("\\.");
                        suffixName = n[n.length - 1];
                        if (!TextUtils.isEmpty(suffixName)) {
                            if (suffixName.equals("docx") || suffixName.equals("doc")) { // word文档
                                course_difficult_point_item_content_img.setImageResource(R.mipmap.btn_toolkit_docx);
                            } else if (suffixName.equals("pdf")) { // pdf
                                course_difficult_point_item_content_img.setImageResource(R.mipmap.btn_toolkit_pdf);
                            } else if (suffixName.equals("xlsx") || suffixName.equals("xls")) { // xlsx
                                course_difficult_point_item_content_img.setImageResource(R.mipmap.btn_toolkit_xlsx);
                            } else if (suffixName.equals("jpeg") || suffixName.equals("jpg") || suffixName.equals("gif") ||
                                    suffixName.equals("png") || suffixName.equals("x-ms-bmp")) { // 图片
                                course_difficult_point_item_content_img.setImageResource(R.mipmap.btn_toolkit_img);
                            } else if (suffixName.equals("pptx") || suffixName.equals("ppt")) {
                                course_difficult_point_item_content_img.setImageResource(R.mipmap.btn_toolkit_ppt);
                            } else {
                                course_difficult_point_item_content_img.setImageResource(R.mipmap.btn_toolkit_unknow);
                            }
                        }
                    }
                }

                course_difficult_point_item_content_name.setText(name);
                course_difficult_point_item_content_ll.addView(contentView);

                difficult_point_item_content_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        CourseDifficultPointSubContent content=(CourseDifficultPointSubContent)view.getTag();
                        String name=content.getName();
                        String url=content.getUrl();
                        String fileName =title+"_"+times+"_"+name;
                        filePath = savePath + fileName; // 文件路径

                        fileUrl = Config.BASE_URL + "/app/study/downloadAttachment?agencyId="
                                + agencyId + "&&url=" + url + "&name=" + name;

                        if (new File(filePath).exists()) { // 如果文件存在
                            DownlaodFile.getUpdateManager().showOpenFileDialog(context, filePath, suffixName);
                        } else {
                            // 下载文件
                            DownlaodFile.getUpdateManager().checkAppUpdate(context, fileUrl, fileName, suffixName);
                        }
                    }
                });

            }
        } else {
            course_difficult_point_item_flg_ll.setVisibility(View.GONE);
        }

        course_difficult_point_item_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (isOpen) {
                    course_difficult_point_item_content_ll.setVisibility(View.GONE);
                    open_file_list_icon_img.setVisibility(View.GONE);
                    isOpen = false;
                    point.setIsShow(false);
                } else {

                    if (list != null && list.size() == 0) {
                        open_file_list_icon_img.setVisibility(View.GONE);
                        course_difficult_point_item_content_ll.setVisibility(View.GONE);
                        point.setIsShow(false);
                    } else {
                        if (list != null && list.size() > 0) {
                            listView.setSelection(position);
                        }
                        point.setIsShow(true);
                        open_file_list_icon_img.setVisibility(View.VISIBLE);
                        course_difficult_point_item_content_ll.setVisibility(View.VISIBLE);
                    }
                    isOpen = true;
                }
            }
        });

        if (point.isShow()) {
            open_file_list_icon_img.setVisibility(View.VISIBLE);
            course_difficult_point_item_content_ll.setVisibility(View.VISIBLE);
        } else {
            open_file_list_icon_img.setVisibility(View.GONE);
            course_difficult_point_item_content_ll.setVisibility(View.GONE);
        }

        course_difficult_point_item_name.setText(times);
    }
}
